﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using EPiServer.Research.MobileCenter.JSON;


namespace iPhoneTesting.WebServices
{
    public partial class TestGadgets : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TryOutWebService testWebServiceHelper = new TryOutWebService();
            testWebServiceHelper.TestAvailableGadgets();
            testWebServiceHelper.CheckGadgetsChildren();

            var gadgetHolder = testWebServiceHelper.GetAllGadgets();

            StringBuilder str = new StringBuilder();

            Response.Write("User info"+ EPiServer.Security.PrincipalInfo.Current.Name);
            Response.Write("<br/>");
            Response.Write(GetRecursive(gadgetHolder.gadgets));
            Response.End();

            
        }

        protected string GetRecursive(List<JSONGadget> gadgets)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<ul>");

            foreach (var child in gadgets)
            {
                str.Append(String.Format("<li><img src=\"{0}\" />", child.url));

                if (child.gadgets.Count > 0)
                {
                    if (child.gadgets[0].type == "Cell")
                    {
                        str.Append(child.name);
                        GetRecursive(child.gadgets);
                    }
                    else
                    {
                        str.Append(String.Format("<a href=\"{0}\" >{1}</a>", child.gadgets[0].url, child.gadgets[0].name));
                    }
                }

                str.Append("</li>");

            }

            str.Append("</ul>");

            return str.ToString();
        }
    }
}
